package com.netposa.datawarehouse.aggregator.config;

import feign.Feign;
import feign.Retryer;
import java.util.concurrent.TimeUnit;
import okhttp3.ConnectionPool;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.cloud.netflix.feign.FeignAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

@Configuration
@ConditionalOnClass({Feign.class})
@AutoConfigureBefore({FeignAutoConfiguration.class})
public class FeignClientConfiguration {
   private static final Logger log = LoggerFactory.getLogger(FeignClientConfiguration.class);

   @Bean
   @Primary
   public OkHttpClient okHttpClient() {
      return (new OkHttpClient.Builder()).readTimeout(60L, TimeUnit.SECONDS).connectTimeout(60L, TimeUnit.SECONDS).writeTimeout(60L, TimeUnit.SECONDS).connectionPool(new ConnectionPool(200, 60L, TimeUnit.SECONDS)).addInterceptor((chain) -> {
         Request request = chain.request();
         Response response = chain.proceed(request);
         log.debug("Request method = {} url = {} Response status = {}", new Object[]{request.method(), request.url(), response.code()});
         return response;
      }).build();
   }

   @Bean
   public Retryer retryer() {
      return new Retryer.Default(1000L, 3000L, 3);
   }
}
